﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnowitSoft
{
    /// <summary>
    /// 封装log4net组件的日志帮助类
    /// </summary>
    public class LogHelper
    {
        #region Static Field

        //level: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF
        private static log4net.ILog log = log4net.LogManager.GetLogger("logger");

        #endregion

        #region WriteLog

        /// <summary>
        /// 将指定的信息写至日志文件内
        /// </summary>
        /// <param name="message">需要写入的信息</param>
        public static void WriteLog(object message)
        {
            if (log.IsDebugEnabled)
                log.Debug(message);
            else if (log.IsInfoEnabled)
                log.Info(message);
            else if (log.IsWarnEnabled)
                log.Warn(message);
            else if (log.IsErrorEnabled)
                log.Error(message);
            else
                log.Fatal(message);
        }

        /// <summary>
        /// 将指定的消息和异常信息同时写入日志文件内
        /// </summary>
        /// <param name="message">需要写入的信息</param>
        /// <param name="exception">需要写入的异常</param>
        public static void WriteLog(object message, Exception exception)
        {
            if (log.IsDebugEnabled)
                log.Debug(message, exception);
            else if (log.IsInfoEnabled)
                log.Info(message, exception);
            else if (log.IsWarnEnabled)
                log.Warn(message, exception);
            else if (log.IsErrorEnabled)
                log.Error(message, exception);
            else
                log.Fatal(message, exception);
        }

        #endregion

        #region WriteLogFormat

        /// <summary>
        /// 将消息以指定的格式写入日志文件
        /// </summary>
        public static void WriteLogFormat(IFormatProvider provider,string format,params object[] args)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat(provider, format, args);
            else if (log.IsInfoEnabled)
                log.InfoFormat(provider, format, args);
            else if (log.IsWarnEnabled)
                log.WarnFormat(provider, format, args);
            else if (log.IsErrorEnabled)
                log.ErrorFormat(provider, format, args);
            else
                log.FatalFormat(provider, format, args);
        }

        /// <summary>
        /// 将消息以指定的格式写入日志文件
        /// </summary>
        public static void WriteLogFormat(string format, object arg0)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat(format, arg0);
            else if (log.IsInfoEnabled)
                log.InfoFormat(format, arg0);
            else if (log.IsWarnEnabled)
                log.WarnFormat(format, arg0);
            else if (log.IsErrorEnabled)
                log.ErrorFormat(format, arg0);
            else
                log.FatalFormat(format, arg0);
        }

        /// <summary>
        /// 将消息以指定的格式写入日志文件
        /// </summary>
        public static void WriteLogFormat(string format, object arg0, object arg1)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat(format, arg0, arg1);
            else if (log.IsInfoEnabled)
                log.InfoFormat(format, arg0, arg1);
            else if (log.IsWarnEnabled)
                log.WarnFormat(format, arg0, arg1);
            else if (log.IsErrorEnabled)
                log.ErrorFormat(format, arg0, arg1);
            else
                log.FatalFormat(format, arg0, arg1);
        }

        /// <summary>
        /// 将消息以指定的格式写入日志文件
        /// </summary>
        public static void WriteLogFormat(string format, object arg0, object arg1, object arg2)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat(format, arg0, arg1, arg2);
            else if (log.IsInfoEnabled)
                log.InfoFormat(format, arg0, arg1, arg2);
            else if (log.IsWarnEnabled)
                log.WarnFormat(format, arg0, arg1, arg2);
            else if (log.IsErrorEnabled)
                log.ErrorFormat(format, arg0, arg1, arg2);
            else
                log.FatalFormat(format, arg0, arg1, arg2);
        }

        /// <summary>
        /// 将消息以指定的格式写入日志文件
        /// </summary>
        public static void WriteLogFormat(string format, params object[] args)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat(format, args);
            else if (log.IsInfoEnabled)
                log.InfoFormat(format, args);
            else if (log.IsWarnEnabled)
                log.WarnFormat(format, args);
            else if (log.IsErrorEnabled)
                log.ErrorFormat(format, args);
            else
                log.FatalFormat(format, args);
        }

        #endregion
    }
}
